/*************************************************
* Title：TinyToolKit
* Author：Opportunity 
* Discribe：Texture纹理的扩展方法
* CreateTime：2020-05-06 12:02:51
* Version：1.0
* Modify Recoder：
*************************************************/
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;

namespace TinyToolKit
{
    public static partial class TinyUtility
    {
        public static class TextureHelper
        {
            /// <summary>
            /// 根据文件后缀，判断是否是图片文件
            /// </summary>
            /// <param name="extension">文件后缀名,如“.jpg”</param>
            /// <returns>如果是图片类型，则返回true</returns>
            public static bool IsImage(string extension)
            {
                string[] imgExtension = { ".jpg", ".JPG", ".png", ".PNG",".tga"};

                return imgExtension.Any(t => extension.Equals(t)); 
            }
            
            /// <summary>
            /// 转化纹理为字节数组
            /// </summary>
            /// <param name="texture">需要保存的纹理</param>
            public static byte[] ConvertTexture2DToPng(Texture2D texture) 
            {
                return texture.EncodeToPNG(); 
            }

            /// <summary>
            /// 转化字节为纹理
            /// </summary>
            /// <param name="bytes">需要转化纹理的字节数组</param>
            /// <returns>返回Texture2D纹理</returns>
            public static Texture2D ConvertByteToTexture2D(byte[] bytes,int width,int height)  
            {
                var tex = new Texture2D(width,height);
                tex.LoadImage(bytes);
                tex.Apply();
                return tex;
            }
        }
    }
}

